package de.bazinga.rotw.statistics;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import com.jmex.game.state.GameStateManager;

import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.util.GlobalSettings;

public class OnlineStatistics {

	/**
	 * Data to be send to the server (Stats)
	 */
	public static int updateScore = 0;
	public static int updateZombiesKilled = 0;
	public static long updateTimeStarted = System.currentTimeMillis();
	
	/**
	 * The time you started defending the base!
	 */
	public static long defendStarted = 0;
	
	
	/**
	 * TreasureHunt-Stuff
	 */
	public static int treasureHuntScore = 0;
	public static long shortestTreasureHuntTime = -1;
	public static long treasureHuntStart = 0;
	
	
	/**
	 * Saves the distance  the player traveled by feet
	 */
	public static float distanceTraveled = 0;
	
	/**
	 * Saves the distance the player traveled by car
	 */
	public static float distanceTraveledVehicle = 0;
	
	
	public static void sendUpdate() {
		
		int seconds = (int)(System.currentTimeMillis() - updateTimeStarted)/1000;

		String username = ((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getProps().getProperty("playerName");
		String gameCode = ((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getProps().getProperty("gameCode");
		
		try {
			URL url = new URL(
					GlobalSettings.STATISTICSURL
					+"?page=saveUpdate&username="+username
					+"&gameCode="+gameCode
					+"&score="+updateScore
					+"&zombiesKilled="+updateZombiesKilled
					+"&time="+seconds
					+"&treasureHuntScore="+treasureHuntScore
					+"&treasureHuntTime="+(shortestTreasureHuntTime/1000)
					+"&distanceTraveled="+(distanceTraveled/2f)
					+"&distanceTraveledVehicle="+(distanceTraveledVehicle/2f));
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String inputLine;
            System.out.println("-----RESPONSE START-----");
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
            in.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			//e.printStackTrace();
		}
		
		
		System.out.println("Update Stats:");
		System.out.println(updateScore);
		System.out.println(updateZombiesKilled);
		System.out.println(treasureHuntScore);
		System.out.println((shortestTreasureHuntTime/1000)+" Sekunden");
		System.out.println((System.currentTimeMillis() - updateTimeStarted)/1000+" Sekunden");
		System.out.println("Traveled by Feet: "+(distanceTraveled/2f));
		System.out.println("Traveled by Vehicle: "+(distanceTraveledVehicle/2f));
		
		reset();
	}
	
	public static void reset() {
		updateScore = 0;
		updateTimeStarted = System.currentTimeMillis();
		defendStarted = System.currentTimeMillis();
	}
	
	public static boolean updateDefendTime() {
		int seconds = (int)(System.currentTimeMillis() - defendStarted)/1000;
		defendStarted = System.currentTimeMillis();

		String username = ((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getProps().getProperty("playerName");
		String gameCode = ((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getProps().getProperty("gameCode");
		
		boolean record = false;
		try {
			URL url = new URL(GlobalSettings.STATISTICSURL+"?page=updateDefendTime&username="+username+"&gameCode="+gameCode+"&defendMaxTime="+seconds);
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String inputLine;
            System.out.println("-----RESPONSE START-----");
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
                if(inputLine.equals("NEW RECORD")) {
                	record = true;
                }
            }
            in.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			//e.printStackTrace();
		}
		
		
		System.out.println("Update Stats:");
		System.out.println(seconds+" Sekunden");
		
		return record;
	}
}
